home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 033a / mxtms_10.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1991-05-27  |  11KB  |  437 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    Misc.c      :Miscellaneous routines for MaxTime                       */
  4. /*                                                                          */
  5. /****************************************************************************/
  6.  
  7. #include "MaxTime.h"
  8. #include "globals.h"
  9.  
  10. int do_reset;
  11. int CurColor;
  12. int Hollercount=0;
  13. int Bells;
  14.  
  15. int timeon(void)
  16. {
  17.    clock_t elapsed;
  18.    int t;
  19.  
  20.    elapsed = clock();
  21.    elapsed = startsecs - elapsed;
  22.    elapsed = elapsed / CLOCKS_PER_SEC;        /* Compute seconds */
  23.    t = (int) (elapsed / 60);
  24.    if (t < 0)
  25.       t = t * (-1);
  26.    return(t);
  27. }
  28.  
  29. void timeremain(void)
  30. {
  31.    int x;
  32.    char tstr[18];
  33.  
  34.    x = LastUser.timeremaining - timeon();        /* Fetch # of minutes on */
  35.    setcolor(TextAttr[TIME_LEFT]);
  36.    sprintf(tstr,"{%d min left} ",x);
  37.    strout(tstr);   
  38. }
  39.  
  40.  
  41. void delay_s(unsigned n)
  42. /* n = Number of seconds */
  43. {
  44.    n *= 1000;        /* Convert to seconds */
  45.    delay_ms(n);
  46. }
  47.  
  48. void delay_ms(unsigned n)
  49.  /* milleseconds to delay */
  50. {
  51.     struct timeb timebuf;
  52.     long end_time;
  53.     unsigned end_millitm;
  54.  
  55.     /* get current time and calculate ending time */
  56.     ftime(&timebuf);
  57.     end_time = timebuf.time + (n / 1000);
  58.     end_millitm = timebuf.millitm + (n % 1000);
  59.     if (end_millitm >= 1000) {
  60.         ++end_time;
  61.         end_millitm -= 1000;
  62.     }
  63.  
  64.     /* loop until ending time reached */
  65.     do ftime(&timebuf);
  66.     while (timebuf.time < end_time ||
  67.           (timebuf.time == end_time && timebuf.millitm < end_millitm));
  68. }
  69.  
  70. int find_realuser(char *name,int handle)
  71. {
  72.    int x;
  73.    struct _usr USER;
  74.  
  75.    lseek(handle,0L,SEEK_SET);
  76.    x = read(handle,(char *) &USER,user_slen);
  77.    while (x == user_slen) {
  78.       if (strcmpi(name,USER.name) == 0) {
  79.          if (USER.flag & UFLAG_deleted)
  80.             return(FALSE);        /* It's there but it is deleted! */
  81.          else return(TRUE);
  82.       }
  83.       x = read(handle,(char *) &USER,user_slen);
  84.    }
  85.    return(FALSE);        /* Couldn't find it */
  86. }
  87.  
  88. /* Strip leading and trailing whitespace */
  89. char *stripwhite(char *strng)
  90. {
  91.    char *p,*pp;
  92.  
  93.    while (*strng && isspace(*strng)) {        /* Trim leading space */
  94.       pp = strng;
  95.       p = strng + 1;
  96.       while (*pp) {        /* Shift up characters */
  97.          *pp = *p;
  98.          p++;
  99.          pp++;
  100.       }
  101.    }
  102.  
  103.    if (!*strng)        /* We have reached end of string */
  104.       return(NULL);
  105.  
  106.    p = strng + (strlen(strng) - 1);        /* Point to tail */
  107.    while (isspace(*p)) 
  108.       p--;
  109.    p[1] = 0;        /* Terminate at start of trailing space */
  110.    return(strng);
  111. }
  112.  
  113. /* Turn on given color attribute for video */
  114. void setcolor(int color)
  115. {
  116.    byte back,fore;
  117.    byte bit2,bit0;
  118.    char attstr[20];
  119.  
  120.    fore = (byte) (color & 0x000f);
  121.    back = (byte) (color & 0x00070);
  122.    back = (byte) (back >> 4);
  123.    sprintf(attstr,"\x1b[1;%d;%dm",back+40,fore+30);
  124.  
  125.    if (IsLocal) {
  126.       printf("\x1b[0m");        /* Reset attributes */
  127.       if (color & 0x0080)         /* Test blink bit */
  128.          printf("\x1b[5m");
  129.       printf(attstr);
  130.    }
  131.    else {
  132.       if (LastUser.ansi) {
  133.          printf("\x1b[0m");        /* Reset attributes */
  134.          FossSendStr("\x1b[0m");        /* Reset attributes */
  135.          if (color & 0x0080) {        /* Test blink bit */
  136.             printf("\x1b[5m");
  137.             FossSendStr("\x1b[5m");
  138.          }
  139.          printf(attstr);
  140.          FossSendStr(attstr);
  141.       }
  142.  
  143.       if (LastUser.avatar) {
  144.          color |= 0x8;        /* Turn on intensity for foreground */
  145.          printf(attstr); /* Print ANSI instead of avatar to local console */
  146.  
  147.    /* Reverse bits 2 and 0 */
  148.          bit2 = (byte) (color & 0x04);
  149.          bit0 = (byte) (color & 0x01);
  150.          color &= 0xfa;        /* Turn off bit 0 and bit 2 */
  151.          color |= (bit0 << 2);
  152.          color |= (bit2 >> 2);
  153.  
  154.          sprintf(attstr,"\x16\x01%c",color);
  155.          FossSendStr(attstr);        /* Send out attribute byte */
  156.       }
  157.    }
  158.  
  159.    CurColor = color;
  160. }
  161.  
  162.  
  163.  
  164. #define TIMERMODE 182        /* code to put timer in right mode */
  165. #define FREQSCALE 1190000L    /* basic time frequency in hertz   */
  166. #define TIMESCALE 1230L        /* number of counts in 0.1 second  */
  167. #define T_MODEPORT 67        /* port controls timer mode        */
  168. #define FREQPORT   66        /* port controls tone frequency    */
  169. #define BEEPPORT   97        /* port controls speaker           */
  170. #define ON         79        /* signal to turn speaker on       */
  171.  
  172. /* Generate a tone of given frequency and length, time is in 10ths of seconds */
  173. void tone (int freq, int time)
  174. {
  175.  
  176.    int  hibyte, lowbyte, port;
  177.    long divisor;
  178.  
  179.    divisor = FREQSCALE / freq;       /* scale freq to timer units  */
  180.    lowbyte = (int) (divisor % 256); /* break integer into         */
  181.    hibyte  = (int) (divisor >> 8);     /*  two bytes                 */
  182.  
  183.    outp (T_MODEPORT, TIMERMODE);    /* prepare timer for input    */
  184.    outp (FREQPORT, lowbyte);    /* set low byte of timer reg  */
  185.    outp (FREQPORT, hibyte); /* set high byte of timer reg */
  186.  
  187.    port = inp (BEEPPORT);        /* save port setting          */
  188.    outp (BEEPPORT, ON);        /* turn speaker on            */
  189.    delay_ms(time);
  190.    outp (BEEPPORT, port);        /* turn speaker off, restore  */
  191.                 /*  original setting          */
  192. }
  193.  
  194.  
  195. void strout(char *outmsg)
  196. {
  197.    if (!IsLocal) {
  198.       check_carrier();
  199.       FossSendStr(outmsg);
  200.       check_carrier();
  201.    }
  202.    printf(outmsg);
  203. }
  204.  
  205.  
  206. void chrout(char ch)
  207. {
  208.    putchar(ch);
  209.    if (!IsLocal) 
  210.       FossSendCh(ch);
  211. }
  212.  
  213.  
  214. void strin(char *inmsg)
  215. {
  216.    unsigned ch;
  217.    int key,minutes;
  218.    char *p;
  219.  
  220.    p = inmsg;
  221.    cflush();
  222.    if (!IsLocal) {        /* Simulate GETS function for fossil */
  223.       minutes = timeon();        /* Establish a counter */
  224.       while (1) {
  225.          check_carrier();
  226.          while ((ch = FossGetCh()) == 0xFFFF) {
  227.             check_carrier();
  228.             if (timeon() > minutes + IDLETIME)        /* User is idle too long */
  229.                aborterror(USERIDLE,NULL);
  230.          }
  231.          key = ch & 0xff;
  232.          if (key == '\r') {        /* Carraige return */
  233.             break;
  234.          }
  235.          if (key == '\b') {        /* Backspace */
  236.             if (p > inmsg) {
  237.                FossSendCh((byte)key);
  238.                putchar(key);
  239.                FossSendCh(' ');
  240.                putchar(' ');
  241.                FossSendCh((byte) key);
  242.                putchar(key);
  243.                p--;
  244.             }
  245.          }
  246.          else {
  247.             (*p) = (char) key;
  248.             p++;
  249.             FossSendCh((byte) key);
  250.             putchar(key);
  251.          }
  252.          *p = 0;        /* Terminate */
  253.       }
  254.       *(p+1) = 0;
  255.    }
  256.    else gets(inmsg);
  257. }
  258.  
  259. int chrin(void)
  260. {
  261.    unsigned ch;
  262.    int minutes;
  263.  
  264.    cflush();
  265.    if (IsLocal) {
  266.       ch = _bios_keybrd(_KEYBRD_READ);
  267.       return(ch & 0xff);        /* Return saying to restart menu */
  268.    }
  269.    else {
  270.       minutes = timeon();        /* Establish a counter */
  271.       while ((ch = FossGetCh()) == 0xFFFF) {
  272.          check_carrier();
  273.          if (timeon() > minutes + IDLETIME) {        /* User is idle too long */
  274.             aborterror(USERIDLE,NULL);
  275.          }
  276.       }
  277.       return(ch);
  278.    }
  279. }
  280.  
  281. int chrinwait(int tminutes)
  282. {
  283.    unsigned ch;
  284.    int minutes;
  285.  
  286.    cflush();
  287.    minutes = timeon();        /* Establish a counter */
  288.    if (IsLocal) {
  289.       while (_bios_keybrd(_KEYBRD_READY)) {
  290.          if (timeon() > minutes + tminutes) {     /* User is idle too long */
  291.             return('\r');
  292.          }
  293.       }
  294.       ch = _bios_keybrd(_KEYBRD_READ);
  295.       return(ch & 0xff);        /* Return saying to restart menu */
  296.    }
  297.    else {
  298.       while ((ch = FossGetCh()) == 0xFFFF) {
  299.          check_carrier();
  300.          if (timeon() > minutes + tminutes) {     /* User is idle too long */
  301.             return('\r');
  302.          }
  303.       }
  304.       return(ch);
  305.    }
  306. }
  307.  
  308. void cflush(void)
  309. {
  310.    while (_bios_keybrd(_KEYBRD_READY))        /* Flush local console */
  311.       getch();
  312.    if (IsLocal)
  313.       return;
  314.    FossPurgeBuff(TRUE);
  315. }
  316.  
  317. int check_carrier(void)
  318. {
  319.    unsigned status;
  320.  
  321.    if (IsLocal) 
  322.       return(FALSE);
  323.  
  324.    status = FossMdmStatus();
  325.    if ((status & 128) == 0) {        /* Look at DCD(carrier bit) */
  326.       aborterror(DROPCARRIER,NULL);
  327.    }
  328.    return(FALSE);
  329. }
  330.  
  331. void logit(char *strng,char flag)
  332. {
  333.    FILE *log;
  334.    char ttemp[40];
  335.    char *ttemp1;
  336.    char *month;
  337.    char *day;
  338.    char *tm;
  339.  
  340.    if (LogFile[0] == 0)
  341.       return;
  342.  
  343.    ttemp1 = (char *) malloc(101);
  344.    if (ttemp1 == NULL) {
  345.       LogMode = FALSE;
  346.       aborterror(BADALLOC,NULL);
  347.    }
  348.  
  349.    strcpy(ttemp, timestring());
  350.    strtok(ttemp," ");               /* Strip Day of week */
  351.    month = strtok(NULL," ");
  352.    day = strtok(NULL," ");
  353.    tm = strtok(NULL," ");
  354.  
  355.    if (Task)
  356.       sprintf(ttemp1,"%c %s %s %s MaxTime{%d}  %s\n",flag,day,month,tm,Task,strng);
  357.    else sprintf(ttemp1,"%c %s %s %s MaxTime  %s\n",flag,day,month,tm,strng);
  358.    if ((log = _fsopen(LogFile,"a",SH_DENYWR)) == NULL) {
  359.       printf("\a\aError opening log file!\n");
  360.       LogMode = FALSE;
  361.       free(ttemp1);
  362.       return;
  363.    }
  364.    fputs(ttemp1,log);
  365.    free(ttemp1);
  366.    fclose(log);
  367. }
  368.  
  369. char *timestring(void)
  370. {
  371.    time_t     aclock;
  372.    struct tm  *newtime;
  373.  
  374.    time(&aclock);
  375.    newtime = localtime(&aclock);
  376.    return(asctime(newtime));
  377. }
  378.  
  379. void signon(void)
  380. {
  381.    char *p;
  382.    struct stat fbuf;
  383.  
  384.    setcolor(TextAttr[HILITE_TEXT]);
  385.  
  386. /* Clear the screen */
  387.    if (LastUser.ansi) 
  388.       strout("\x1b[2J");
  389.    else if (LastUser.avatar) {
  390.       printf("\x1b[2,J");        /* Always send ansi to local console */
  391.       FossSendStr("\x0c");        /* ^L */
  392.    }
  393.  
  394.    sprintf(temp,"Begin, v%2.2f",Version);
  395.    logit(temp,'+');
  396.    if (!newuser) 
  397.       sprintf(temp,"%s logging on",LastUser.name);
  398.    else sprintf(temp,"New User: %s logging on",LastUser.name);
  399.    logit(temp,':');
  400.  
  401.    strout("MaxTime - a MAXIMUS timebank utility by Craig Derouen\r\n");
  402.    sprintf(temp,"\t *** Version %.2f ***\r\n",Version);
  403.    strout(temp);
  404.  
  405.    setcolor(TextAttr[STD_TEXT]);
  406.    strcpy(temp1,LastUser.name);
  407.    p = strtok(temp1," ");        /* Fetch 1st name only, shows how friendly we are */
  408.    if (USERCFG.calls) {
  409.       sprintf(temp,"\r\nWelcome back to MaxTime, %s.\r\n",p);
  410.       strout(temp);
  411.    }
  412.    else {
  413.       sprintf(temp,"\r\nWelcome to MaxTime, %s.\r\n",p);
  414.       strout(temp);
  415.       newuser_help();        /* Display a file to the new user */
  416.   }
  417.  
  418. /* Now test filedate on Bulletin file. If it is newer than last logon
  419.    of user, display it. */
  420.  
  421.    if(stat(BullFile,&fbuf) == 0 ) {        /* File does exist */
  422.       if (fbuf.st_mtime >= USERCFG.lasttime || (USERCFG.calls == 0))    {    /* See if changed since last logon */
  423.          logit("User is viewing bulletin file",'#');
  424.          setcolor(TextAttr[STD_TEXT]);
  425.          strout("\r\nBulletins:\r\n");
  426.          if(show_file(BullFile) >= 0) {
  427.             setcolor(TextAttr[HILITE_TEXT]);
  428.             strout("\r\n\r\nPress any key to continue logon.");
  429.             chrinwait(1);
  430.             strout("\r\n");
  431.          }
  432.       }
  433.    }
  434. }
  435.  
  436.  
  437.